package com.qf.service;

import com.qf.constant.CacheConstants;
import com.qf.dfa.SensitiveWordFilter;
import com.qf.dfa.SensitiveWordInit;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.util.SensitiveWordUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * author:z
 * time:2021/12/29 17:22
 * description:敏感词过滤策略
 */
@Service(value = "dirtyWords")
@Slf4j
@ComponentScan(value = "com.qf.dfa")
public class DirtyWordsChainManagerImpl implements FilterChainService {
    @Autowired
    CacheFeign cacheFeign;

//    @Resource
//    SensitiveWordFilter sensitiveWordFilter;
//
//    @Resource
//    SensitiveWordInit sensitiveWordInit;

    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        log.info("[策略模块-敏感词过滤策略]开始执行！");
        //1.调用工具包 获取短信内容中的敏感词 *表示通配符
        Set<String> dirtyWords = cacheFeign.keys(CacheConstants.CACHE_PREFIX_DIRTY_WORDS + "*");
        //2.将短信中的敏感词替换为空
        String replace =null;
        for (String dirtyWord : dirtyWords) {
            replace=dirtyWord.replace("DIRTYWORDS:", "");
        }
        Set<String> word=new HashSet<>();
        word.add(replace);
        //3.初始化敏感词
        SensitiveWordUtil.init(word);
        //4.判断工具类中包含的敏感词
        SensitiveWordUtil.contains(standardSubmit.getMessageContent());
        //5.查看工具类中的敏感词是否包含短信中的敏感词 添加到集合中
        Set<String> di1rtyWords = SensitiveWordUtil.getSensitiveWord(standardSubmit.getMessageContent());
        log.info("[策略模块-敏感词过滤策略] 敏感词校验成功 短信中包含敏感词的个数为：{}", word.size());

        //将工具类实例化
//        SensitiveWordFilter instance = SensitiveWordFilter.getInstance();
//        //调用工具类中的方法判断短信中是否包含敏感词
//        int dirtywords = instance.checkSensitiveWord(standardSubmit.getMessageContent(), 0, 2);
//        //如果敏感词为空的时候 不拦截 输出
//        if(dirtywords==0) {
//            log.info("[策略模块-敏感词过滤策略] 敏感词校验成功 短信中包含敏感词的词为：{}", standardSubmit.getMessageContent());
//            return true;
//        }else{
//            //如果不为空的时候 就拦截
//            log.info("[策略模块-敏感词过滤策略] 敏感词校验成功 短信中包含敏感词的词为：{}", standardSubmit.getMessageContent());
//        }
        return false;
        //Set<String> word = dirtyWords.stream().map(s -> s.replace("DIRTYWORDS:", "")).collect(Collectors.toSet());
//        SensitiveWordInit sensitiveWordInit = new SensitiveWordInit();
//        SensitiveWordFilter sensitiveWordFilter = (SensitiveWordFilter)r applicationContext.getBean("sensitiveWordFilter");
//         int checkResult = sensitiveWordFilter.checkSensitiveWord(standardSubmit.getMessageContent(), 0, 2);
//        //2.判断敏感词数量 如果为空 返回true 可以发送短信
//        //不能发送短信,checkResult
    }
}
